package com.bum.api.core.util.render;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.math.RandomUtils;

import com.bum.base.util.Base64Utils;

public class MeRenderUtil {
//	private static final String	RSS_REGEX		= "(rss|Rss|RSS)://((?:[a-z0-9]+[a-zA-Z0-9\\-_/\\.]+))(\\b|\\s)";
//	private static final String	RSS_HTML		= "<a href=\"#\" rel=\"rss\">$0</a>";
//
//	private static final String	CLAN_REGEX		= "\\B#((?:[a-zA-Z0-9][a-zA-Z0-9_.]*[a-zA-Z0-9]))";
//	private static final String	CLAN_HTML		= "#<a href=\"#\" rel=\"clan\">$1</a>";

	public static final String	NICK_REGEX		= "\\B@((?:[a-zA-Z][a-zA-Z0-9_]*[.]?[a-zA-Z0-9_]*[a-zA-Z0-9]))";
	public static final String	NICK_HTML		= "@<a href=\"#\" rel=\"nick\">$1</a>";

	private static final String	EMAIL_REGEX		= "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}";
	private static final String	EMAIL_HTML		= "<a href=\"mailto: $0\" rel=\"email\">$0</a>";

	private static final String	URL_REGEX		= "(?:(?:ht|f)tp(?:s?)://)(?:(?:[-\\w]+\\.)+(?:[-\\w]+))(?::[\\d]{1,5})?(?:(?:(?:/(?:[-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|/)+|\\?|#)?(?:(\\S)*)";
	private static final String	URL_HTML		= "<a href=\"#\" rel=\"http\">$0</a>";

	public static final String	HTML_LINEBREAK	= " <br/> ";

	public static final String	MEDIA_PREFIX	= "##";
	private static final String	MEDIA_REGEX		= "\\B\\##((?:[a-zA-Z0-9_\\.\\-]+))\\#";
	
	static Pattern				mediaPattern	= Pattern.compile(MEDIA_REGEX);

	public static String safeHtml(String html) {
		if (html == null) {
			return html;
		}
		return html.replaceAll("&((?:[a-zA-Z])+)", "&amp;$1").replaceAll("<([/]?.*?)>", "&lt;$1&gt;");
	}

	public static String makeUrlTags(String content) {
		if (content == null) {
			return null;
		}
		return content.replaceAll(URL_REGEX, URL_HTML).replaceAll(EMAIL_REGEX, EMAIL_HTML);
	}

	// public static String renderContent2(String content) {
	// return renderContent(content, false);
	// }

	public static String renderContent(String content) {
		String html = content;
		if (html == null) {
			return null;
		}

//		html = safeHtml(html);
		html = html.replaceAll("[\\n\\r]+", HTML_LINEBREAK);

//		html = html.replaceAll(URL_REGEX, URL_HTML);
		html = RenderUtil.renderHttp(html);
		html = html.replaceAll(EMAIL_REGEX, EMAIL_HTML);

//		html = SmileyUtil.formatSmileys(html);

		if (html.indexOf(MEDIA_PREFIX) > -1) {
			html = showMedia(html);
		}

//		if (html.indexOf("#") > -1) {
//			html = html.replaceAll(CLAN_REGEX, CLAN_HTML);
//		}
//
//		if (html.toLowerCase().indexOf("rss://") > -1) {
//			html = (html + " ").replaceAll(RSS_REGEX, RSS_HTML).trim();
//		}

		html = EmojiBO.convert(html);

		return html;
	}

	static String showMedia(String html) {
		StringBuffer result = new StringBuffer();

		String rel = "m" + RandomUtils.nextInt(1000);

		Matcher matcher = mediaPattern.matcher(html);
		while (matcher.find()) {
			String mediaCode = matcher.group(1);
			String mediaHtml = MediaCodeUtil.displayMediaAsHtml(mediaCode, rel);
			if (mediaHtml != null) {
				mediaCode = mediaHtml;
			}
			matcher.appendReplacement(result, mediaCode);
		}
		matcher.appendTail(result);

		return result.toString();
	}
	
	public static void main(String[] args) {
		System.out.println(new String(Base64Utils.decode("LCF8Ngw3Y-82JuA3MEmxYua3MGmpOuA8JIV2JIa4Pgm8N784WvF8cihuNvjmLCF8Nfj6aiapWiV9ZuJtZrj6Kut1K69wJ-82ZrFwN.FpOBtrYua3MGj6aiWuOIe0M-AsWiV9ZuJ6YiV4ZidtZrj6Kut1JEOtN6j6aiWuZrH5af84aq84a6j6aiWuZid4Zrh5arjSZq97JEm-LED8YG86YGTyNBN_")));
	}
}
